//
//  YXImageLayout.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/17.
//

#import "YXImageLayout.h"

@implementation YXImageLayout

- (instancetype)init {
    if (self = [super init]) {
        self.verticalFillType = YXImageFillTypeCompletely;
        self.horizontalFillType = YXImageFillTypeFullWidth;
        self.zoomScaleSurplus = 1.5;
    }
    return self;
}

#pragma mark - private

- (YXImageFillType)fillTypeByOrientation:(UIDeviceOrientation)orientation {
    return UIDeviceOrientationIsLandscape(orientation) ? self.horizontalFillType : self.verticalFillType;
}

#pragma mark - YXImageLayoutProtocol -

/**
 计算图片展示的位置

 @param containerSize 容器大小
 @param imageSize 图片大小 (逻辑像素)
 @param orientation 图片浏览器的方向
 @return 图片展示的位置 (frame)
 */
- (CGRect)yx_imageViewFrameWithContainerSize:(CGSize)containerSize imageSize:(CGSize)imageSize orientation:(UIDeviceOrientation)orientation {
    if (containerSize.width <= 0 ||
        containerSize.height <= 0 ||
        imageSize.width <= 0 ||
        imageSize.height <= 0) {
        return CGRectZero;
    }
    
    CGFloat x = 0, y = 0, width = 0, height = 0;
    switch ([self fillTypeByOrientation:orientation]) {
        case YXImageFillTypeFullWidth: // 宽度自适应优先填充
        {
            x = 0;
            /// 获取容器宽度
            width = containerSize.width;
            /// 获取图片在容器中的最大高度
            height = containerSize.width * (imageSize.height / imageSize.width);
            /// 判断图片宽高比例是否超出容器
            if (imageSize.width / imageSize.height >= containerSize.width / containerSize.height)
                y = (containerSize.height - height) / 2.0;
            else
                y = 0;
        }
            break;
        case YXImageFillTypeCompletely: {
            if (imageSize.width / imageSize.height >= containerSize.width / containerSize.height) {
                width = containerSize.width;
                height = containerSize.width * (imageSize.height / imageSize.width);
                x = 0;
                y = (containerSize.height - height) / 2.0;
            } else {
                height = containerSize.height;
                width = containerSize.height * (imageSize.width / imageSize.height);
                x = (containerSize.width - width) / 2.0;
                y = 0;
            }
        }
            break;
            
        default:
            return CGRectZero;
    }
    return CGRectMake(x, y, width, height);
    
}


- (CGFloat)yx_maximumZoomScaleWithContainerSize:(CGSize)containerSize imageSize:(CGSize)imageSize orientation:(UIDeviceOrientation)orientation {
    if (self.maxZoomScale >= 1) return self.maxZoomScale;
    
    if (containerSize.width <= 0 || containerSize.height <= 0) return 0;
    
    CGFloat scale = [UIScreen mainScreen].scale;
    if (scale <= 0) return 0;
    
    CGFloat widthScale = imageSize.width / scale / containerSize.width,
    heightScale = imageSize.height / scale / containerSize.height,
    maxScale = 1;
    switch ([self fillTypeByOrientation:orientation]) {
        case YXImageFillTypeFullWidth:
            maxScale = widthScale;
            break;
        case YXImageFillTypeCompletely:
            maxScale = MAX(widthScale, heightScale);
            break;
        default: return 0;
    }
    return MAX(maxScale, 1) * self.zoomScaleSurplus;
}


@end
